package com.x.mvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.Toast;

import com.x.mvp.injection.components.DaggerFragmentComponent;
import com.squareup.leakcanary.RefWatcher;
import com.x.mvp.R;
import com.x.mvp.MVPApplication;
import com.x.mvp.injection.components.ActivityComponent;
import com.x.mvp.injection.components.FragmentComponent;
import com.x.mvp.widget.AlartDialog;
import com.x.mvp.widget.AppProgress;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    protected View mainView;
    private Unbinder unbinder;
    private boolean isVisibleToUser;
    private boolean isViewCreated = false;
    private boolean isPageRunning;

    private ActivityComponent activityComponent;
    private FragmentComponent fragmentComponent;

    private ViewStub tipsViewStub;
    private View tipsView;

    private AppProgress appProgress;
    private AlartDialog alartDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    // Should call after activity created
    @Deprecated
    protected ActivityComponent getActivityComponent() {
        try {
            if (activityComponent == null) {
                activityComponent = ((BaseActivity) getActivity()).getActivityComponent();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activityComponent;
    }

    protected FragmentComponent getFragmentComponent() {
        try {
            if (fragmentComponent == null) {
                fragmentComponent = MVPApplication.getInstance().getFragmentComponent(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fragmentComponent;
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected void initData(@Nullable Bundle arguments) {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mainView == null) {
            mainView = inflater.inflate(getLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, mainView);
            initView();
            isViewCreated = true;
            initData(getArguments());
        }
        return mainView;
    }

    @Override
    public void onResume() {
        super.onResume();
        isPageRunning = true;
        if (getUserVisibleHint()) {
            onUserVisible(true);
        }
    }

    @Override
    public void onPause() {
        onUserVisible(false);
        isPageRunning = false;
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        Fragment fragment = getTargetFragment();
        if (fragment != null && fragment instanceof BaseFragment) {
            BaseFragment targetFragment = (BaseFragment) fragment;
            targetFragment.onFragmentResult(getTargetRequestCode());
        }
        if (mainView != null && mainView.getParent() != null) {
            ((ViewGroup) mainView.getParent()).removeView(mainView);
        }
        super.onDestroy();
        RefWatcher refWatcher = MVPApplication.getRefWatcher(getActivity());
        refWatcher.watch(this);
    }

    protected void onFragmentResult(int requestCode) {
    }

    protected boolean isVisibleToUser() {
        return isVisibleToUser;
    }

    protected boolean isViewCreated() {
        return isViewCreated;
    }

    protected boolean isPageRunning() {
        return isPageRunning;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        onUserVisible(isVisibleToUser);
    }

    protected void onUserVisible(boolean isVisibleToUser) {
    }

    protected View getTipsViewFromActivity() {
        return ((BaseActivity) getActivity()).getTipsView();
    }

    protected void initTipsView() {
        tipsView = getTipsViewFromActivity();
        if (tipsView == null) {
            if (getTipsStubViewId() > 0) {
                tipsViewStub = (ViewStub) mainView.findViewById(getTipsStubViewId());
                if (getTipsRootViewLayoutId() > 0) {
                    tipsViewStub.setLayoutResource(getTipsRootViewLayoutId());
                    tipsView = tipsViewStub.inflate();
                }
            }
        }
    }

    public int getTipsStubViewId() {
        return R.id.tips;
    }

    public int getTipsRootViewLayoutId() {
        return R.layout.mvp_layout_coordinator;
    }


    public void showTips(int resId) {
        toast(resId, Toast.LENGTH_SHORT);
    }

    public void showTips(String msg) {
        toast(msg);
    }

    public void toast(int resId, int length) {
        Toast.makeText(getContext(), getString(resId), length).show();
    }

    public void toast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    protected AppProgress newAppProgress() {
        return AppProgress.getAppProgress(getContext(), true, null);
    }

    public void showAppProgress() {
        if (appProgress == null) {
            appProgress = newAppProgress();
        }
        appProgress.show();
    }

    public void hideAppProgress() {
        if (appProgress != null) {
            appProgress.dismiss();
        }
    }

    protected AlartDialog newAlartDialog() {
        return new AlartDialog(getContext());
    }

    public void showAlartDialog(int type, String message, int time) {
        if (alartDialog == null) {
            alartDialog = newAlartDialog();
            alartDialog.setiDissmiss(iDissmiss);
        }
        alartDialog.showMessage(type, message, time);
    }

    public void hideAlartDialog() {
        if (alartDialog != null) {
            alartDialog.dismiss();
        }
    }

    public AlartDialog.IDissmiss iDissmiss = new AlartDialog.IDissmiss() {
        @Override
        public void onDismiss(int type) {
            dismissAlartDialog(type);
        }
    };

    public void dismissAlartDialog(int type){

    }



}